﻿using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace TodoApp.EntityFrameworkCore.Core
{
    public class MyCustomizer : ModelCustomizer
    {
        public IServiceProvider Provider { get; } 

        public MyCustomizer(ModelCustomizerDependencies dependencies, IServiceProvider provider) : base(dependencies)
        {
            Provider = provider; 
        }

        public override void Customize(ModelBuilder modelBuilder, DbContext context)
        {
            base.Customize(modelBuilder, context);
            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            {
                foreach (var conf in Provider.GetServices<IMyModelCustomizer>())
                {
                    conf.Configure(entityType);
                }
            }
        }
    }
}
